﻿using System.Collections.Generic;
using System.Web.Mvc;
using WxxMail.BLL;
using WxxMail.Common;
using WxxMail.Domain;
using WxxMail.IBLL;
using WxxMail.MVC.Factories;
using WxxMail.MVC.Models;

namespace WxxMail.MVC.Controllers
{
    public class HomeController : Controller
    {
        //IAnswerService _AnswerService = new AnswerService();
        //public ActionResult Index(AnswerRequest request)
        //{

        //    var result = _AnswerService.GetList(request);
        //    return View(result);
        //    //var db = new ArticleEntities();
        //    //return View(db.Answer);

        //}
       
        IAnswerFactory _answerFactory= new AnswerFactory();
    [Authorize]
        public ActionResult Index(AnswerRequest Request)
        {
            var answerListModel = _answerFactory.PrepareAnswerListModel(Request);
            return View(answerListModel);
        }
        /// <summary>
        /// 返回AddView视图
        /// </summary>
        /// <returns></returns>
        public ActionResult AddView()
        {
            return View();
        }
        /// <summary>
        /// 添加Answer
        /// </summary>
        /// <param name="answermodel"></param>
        /// <returns></returns>
        public ActionResult AddAnswer(AnswerModel answermodel)
       {
            //            _answerService.AddAnswer(answer);
            if (ModelState.IsValid)
            {
                _answerFactory.AddAnswer(answermodel);
                return RedirectToAction("Index");
            }
            else
            {
                return View("AddView");

            }
           
        }
       /// <summary>
       /// 返回一需要编辑的数据页面
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
        public ActionResult Edit(int id)
        {
            var model = _answerFactory.GetEditAnswer(id);
            return View(model);
            //var model = this.CmsService.GetArticle(id);

            //var channelList = this.CmsService.GetChannelList(new ChannelRequest() { IsActive = true });
            //this.ViewBag.ChannelId = new SelectList(channelList, "ID", "Name");
            //this.ViewBag.Tags = this.CmsService.GetTagList(new TagRequest() { Top = 20, Orderby = Orderby.Hits });

            //return View(model);
        }
        [HttpPost]
        public ActionResult Edit(FormCollection formCollection)
        {
            _answerFactory.save(formCollection);
            return RedirectToAction("Index");
        }

    }
}